{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit DirectShowRadio;

interface

uses DirectShowAudio, DirectShow9;

const
  RADIO_GRAPH_FILENAME = 'Radio';

type
  TAHMDirectShowRadio = class(TAHMDirectShowAudio)
  protected
    FRadio: IBaseFilter; // Audio Input Filter
    procedure CustomGraphConfiguration; override;
    procedure CustomGraphBuild; override;
    procedure CustomGraphShutdown; override;
  end;

implementation

uses
  SysUtils, Logging, ApplicationManager, DirectShowEnumerator, AHMTypes;

procedure TAHMDirectShowRadio.CustomGraphConfiguration;
var
  FilterName: String;
  Enumerator: TAHMDirectShowEnumerator;
begin
  // Call inherited method to add output device
  inherited;

  // Add custom audio capture source to graph if one specified in preferences
  Enumerator := TAHMDirectShowEnumerator.Create;
  try
    // Get preferred/default audio capture source name
    FilterName := ApplicationManagerInstance.PreferencesManager.AudioCapture;
    if FilterName <> '' then
      FRadio := Enumerator.CreateAudioCaptureSource(FilterName)
    else
      FRadio := Enumerator.CreateDefaultRadioSource(FilterName);

    if (FRadio <> nil) and FGraph.AddFilter(FRadio, FilterName) then
      LogInfo('Added custom audio input source to graph: ' + FilterName)
    else
      LogWarn('Failed to add custom audio input source to graph: ' + FilterName);
  finally
    Enumerator.Free;
  end;
end;

procedure TAHMDirectShowRadio.CustomGraphBuild;
var
  CapturePin: IPin;
begin
  LogInfo('Building custom DirectShow graph for rendering Radio');

  // First, we'll need an FM Radio filter to capture audio input
  if FRadio = nil then
    raise EDirectShowException.Create('Failed to locate Radio filter in DirectShow graph');

  // Locate capture output pin
  CapturePin := FGraph.FindPinByName(FRadio, 'Capture');

  // Verify each pin and render it to complete graph connections
  if CapturePin = nil then
    raise EDirectShowException.Create('Failed to find Radio "Capture" pin in DirectShow graph');
  if not FGraph.RenderPin(CapturePin) then
    raise EDirectShowException.Create('Failed to render Radio "Capture" pin - check device settings');

  LogInfo('Finished building custom DirectShow graph for rendering Radio');

  // Save the graph for analysis later
  FGraph.SaveGraph(RADIO_GRAPH_FILENAME);
end;

procedure TAHMDirectShowRadio.CustomGraphShutdown;
begin
  // Free Radio specific interfaces
  FRadio := nil;

  // Call inherited method to remove audio interfaces
  inherited;
end;


end.
